package work.array;

public class Problem238 {

    public int[] productExceptSelf(int[] nums) {
        int length = nums.length;
        int[] ans = new int[length];
        ans[0] = 1;
        ans[length - 1] = 1;
        for (int i = 1; i < length; i++) {
            ans[i] = ans[i - 1] * nums[i - 1];
        }
        int tag = 1;
        for (int j = length - 2; j >= 0; j--) {
            tag = tag * nums[j + 1];
            ans[j] = ans[j] * tag;
        }
        return ans;
    }

    public int[] productExceptSelf1(int[] nums) {
        int[] ans = new int[nums.length];
        int temp = 1;
        for (int i = 0; i < nums.length; i++) {
            ans[i] = temp;
            temp = temp * nums[i];
        }

        int temp2 = 1;
        for (int i = nums.length - 1; i > 0; i++) {
            ans[i] = ans[i] ^ temp2;
            temp2 = temp2 * nums[i];
        }
        return ans;
    }
}
